/**
 * 系统用户修改js
 */
var comValidform=null;
var returnStatus=false;
var sysUserAdd={
	dataCache:null,
	duties:null,
	
	startEvent:function(){
		//获取所有职务
		var url = 'mgr/OrganizationCtrl/listAll';
		var params = null;

		var cb = function(data) {
			console.trace("data code = " & data.resultCode);
			if (data.resultCode == 200) {
				sysUserAdd.duties = data.result;
			}
		};
		sendApiNoLoadSync(url, params, cb, 'GET');
		
		//展现职务
		sysUserAdd.showInfoById();
		
		//更改职务提交
		comValidform=$(".com-addForm").Validform({
			tiptype:2,
			tipSweep:true,
			beforeSubmit:function(curform){
				var checkedId='';
				$('.com-addForm').find('input:checkbox[name="roleList"]:checked').each(function(){
					var checkedVal=$(this).val();
					checkedId+=checkedVal.split(':')[0]+',';
				});
				if(checkedId!='')
					checkedId=checkedId.substring(0,checkedId.length-1);
				$('.com-addForm input[name="sysRoleIds"]').val(checkedId);
				
				var url = 'mgr/SysUserCtrl/saveSysUser';
				var params = jsUtils.serializeJson($('.com-addForm'));
				params.actUserId=userId;
				delete params.roleList;



				if(params.name.length != 11){
					alert("手机号码不是11位，用户添加失败！");
					return false;
				}


				var cb = function(data){
					alert(data.msg);
					//成功返回
					if(data.resultCode==200){
						//关闭窗口并刷新数据
						art.dialog.close();
						returnStatus=true;
					}
				};
				sendApiNoLoadSync(url, params, cb, 'POST');
				return false;
			}
		});
	},
	showDuties:function(){
		var setting = {
				check : {
					enable : true,
					chkStyle : "radio",
					radioType : "all"
				},
				data : {
					simpleData : {
						enable : true
					}
				},
				view : {
					nameIsHTML : true
				},
				callback : {
					onCheck : zTreeOnClick,
					beforeCheck: zTreeBeforeCheck
				}
			};
			var zNodes = [];
			var duties = sysUserAdd.duties;
			
			for (var i = 0; i < duties.length; i++) {
				zNodes[i] = {
					id : duties[i].id,
					name : duties[i].name,
					pId : duties[i].parentID
				};
				if (i == 0) {
					zNodes[i].open = true;
				}
			}
			$.fn.zTree.init($("#dutTree"), setting, zNodes);
	},
	showRole:function(){
		//获取所有角色
		var url ='mgr/SysRoleCtrl/getSysRoleList';
		var params={
			'curPage':0,
			'pageSize':9999
		};
		var cb = function(data){
			sysUserAdd.dataCache=data.result;
			var rolesCache=sysUserAdd.dataCache;
			var tr='';
			for(var i=0;i<rolesCache.length;i++){
				if(i%4==0){
					tr+='<tr style="height: 25px;">';
				}
				var id=rolesCache[i].sysRole.id;
				var name=rolesCache[i].sysRole.name;
				tr+='<td><input type="checkbox" name="roleList" value="'+id+':'+name+'" />'+name+'</td>';
				if(i%4==3){
					tr+='</tr>';
				}
			}
			$('#roleTable').append(tr);
		};
		sendApiNoLoad(url, params, cb, 'POST');
	},
	
	showInfoById:function(){
		sysUserAdd.showDuties();
		sysUserAdd.showRole();
	},
	
	init:function(){
		sysUserAdd.startEvent();
	}
}
$(document).ready(function() {
	sysUserAdd.init();
});
function submitForm(){
	comValidform.submitForm();
	return returnStatus;
}

function zTreeOnClick(event, treeId, treeNode) {
	$("#orgId").val(treeNode.id);
};

function zTreeBeforeCheck(treeId, treeNode, clickFlag) {
	
	if(treeNode.isParent){
		alert("请选择具体的职务！");
	}
	
	return !treeNode.isParent;//当是父节点 返回false 不让选取
}; 